package com.weaver.qfengx;

import java.text.DecimalFormat;

/**
 * 数值相关工具类
 * Created by YeShengtao on 2020/10/12 11:09
 */
public class NumberUtils {

    public static String doubleVal(Double number) {
        return doubleVal(number, "#0.00");
    }
    public static String doubleVal(Double number, String pattern) {
        DecimalFormat df = new DecimalFormat(pattern);// 最多保留几位小数，就用几个#，最少位就用0来确定
        return df.format(number);
    }

    public static void main(String[] args) {
        System.out.println(doubleVal(123.1251313132D));
        System.out.println(doubleVal(0.1231313132D));
        System.out.println(doubleVal(2131313131D));
    }

    public static int parseInt(String number, int defaultVal) {
        if (StringUtils.isEmpty(number)) {
            return defaultVal;
        }
        try {
            return Integer.parseInt(number);
        } catch (Exception e) {
            return defaultVal;
        }
    }

    public static int parseInt(String number) {
        return parseInt(number, 0);
    }
    public static Integer parseInteger(String number) {
        return parseInteger(number, null);
    }

    public static Integer parseInteger(String number, Integer defaultVal) {
        if (StringUtils.isEmpty(number)) {
            return defaultVal;
        }
        try {
            return Integer.getInteger(number);
        } catch (Exception e) {
            return defaultVal;
        }
    }

}
